package com.crm.dataReceive.controller;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.crm.custinfo.domain.HitPackage;
import com.crm.custinfo.service.IHitPackageService;
import com.crm.dataReceive.domain.CheckApi;
import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.utils.AesUtils;
import com.ruoyi.common.utils.Md5Encrypt;
import org.apache.commons.lang.StringUtils;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import sun.applet.Main;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/customer")
public class CustPrefix8CheckController extends BaseController {
    public static final org.slf4j.Logger logger = LoggerFactory.getLogger("CustPrefix8CheckInfo");
    @Autowired
    private IHitPackageService hitPackageService;
    @Anonymous
    @PostMapping("/check/tenant/{tenantId}")
    public Map<String, Object> check(@RequestBody CheckApi checkApi) {
        Map<String, Object> map = new HashMap<>();
        if (checkApi == null) {
            map.put("code", 500);
            map.put("msg", "请求包数据为空");
            map.put("data",new String[]{});
            return map;
        }
        String data = checkApi.getData();
        try {
            data=AesUtils.decryptPKCS5Padding(data, "fGYv8HfEdb9cStYn");
        }catch (Exception e) {
            map.put("code", 500);
            map.put("msg", "解密失败");
            map.put("data",new String[]{});
            return map;
        }
        JSONObject json = JSON.parseObject(data);
        String phoneNo = json.getString("phone7");
        if (StringUtils.isBlank(phoneNo)) {
            map.put("code", 500);
            map.put("msg", "手机号为空");
            map.put("data",new String[]{});
            return map;
        }
        HitPackage hitPackage = new HitPackage();
        hitPackage.setMobilePrefix7md5(Md5Encrypt.md5(phoneNo.substring(0,7)));
        List<HitPackage> list1= hitPackageService.selectHitPackageList(hitPackage);
        if(!list1.isEmpty()){
            map.put("code", 200);
            map.put("msg", "成功");
            map.put("data",list1.stream()
                    .map(HitPackage::getMobileMd5)
                    .toArray(String[]::new));
            return map;
        }
        map.put("code", 200);
        map.put("msg", "成功");
        map.put("data",new String[]{});
        return map;
    }
    @Anonymous
    @PostMapping("/checkAllNumbers/tenant/{tenantId}")
    public Map<String, Object> checkAllNumbers(@RequestBody CheckApi checkApi) {
        Map<String, Object> map = new HashMap<>();
        if (checkApi == null) {
            map.put("code", 500);
            map.put("msg", "请求包数据为空");
            return map;
        }
        String data = checkApi.getData();
        try {
            data=AesUtils.decryptPKCS5Padding(data, "fGYv8HfEdb9cStYn");
        }catch (Exception e) {
            map.put("code", 500);
            map.put("msg", "解密失败");
            return map;
        }
        JSONObject json = JSON.parseObject(data);
        String phoneNo = json.getString("phone");
        if (StringUtils.isBlank(phoneNo)) {
            map.put("code", 500);
            map.put("msg", "手机号为空");
            return map;
        }
        if(phoneNo.length()!=32){
            map.put("code", 500);
            map.put("msg", "手机号md5非32位md5格式");
            return map;
        }
        HitPackage hitPackage = new HitPackage();
        hitPackage.setMobileMd5(phoneNo);
        List<HitPackage> list1= hitPackageService.selectHitPackageList(hitPackage);
        if(!list1.isEmpty()){
            map.put("code", 500);
            map.put("msg", "号码已存在");
            return map;
        }
        map.put("code", 200);
        map.put("msg", "成功");
        return map;
    }

    public static void main(String[] args) {
        String data="{\"phone\":\"c5f3ec33f1ef1f1f94fb8e4fdd2wwec5\"}";
        try {
            String s = AesUtils.encrypt(data, "fGYv8HfEdb9cStYn");
            System.out.println(s);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
